package com.tool.runner;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 * 代码生成器演示
 * </p >
 * @author hubin
 * @date 2016-12-01
 */
public class MybatisGenerator {
    private static String driver = "org.postgresql.Driver";
    private static String url = "jdbc:postgresql://172.16.11.47:9999/links_240516?currentSchema=public";
    private static String username = "postgres";
    private static String password = "postgres";
    private static String schemaName = "public";

    /**
     * <p>
     * 读取控制台内容
     * </p >
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("temp");
        gc.setFileOverride(true);
        gc.setSwagger2(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setDateType(DateType.ONLY_DATE);
        gc.setAuthor("tyz");

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setEntityName("%s");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceImplName("%sService");
        //gc.setServiceName("%sService");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.POSTGRE_SQL);
        dsc.setTypeConvert(new PostgreSqlTypeConvert() {
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });
        dsc.setDriverName(driver);
        dsc.setUrl(url);
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setSchemaName(schemaName);
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
//        strategy.setTablePrefix(new String[]{"pib_"});// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
//        strategy.setInclude(scanner("表名，多个英文逗号分割").split(",")); // 需要生成的表
        strategy.setEntityLombokModel(true);
        strategy.setTablePrefix("links_");
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("createBy", FieldFill.INSERT));
        tableFillList.add(new TableFill("createdAt", FieldFill.INSERT));
        tableFillList.add(new TableFill("updateBy", FieldFill.UPDATE));
        tableFillList.add(new TableFill("updatedAt", FieldFill.UPDATE));

        strategy.setTableFillList(tableFillList);
        strategy.setRestControllerStyle(true);
        mpg.setStrategy(strategy);

        // 打印注入设置【可无】
        //    System.err.println(mpg.getCfg().getMap().get("abc"));


        // 包配置
        PackageConfig pc = new PackageConfig();
        mpg.setPackageInfo(pc);
        pc.setParent("com.dimensionx.links");
        pc.setEntity("mapper");
        pc.setEntity("service");
        pc.setEntity("entity");
        pc.setController("controller");


        // 关闭默认 xml 生成，调整生成 至 根目录
        TemplateConfig tc = new TemplateConfig();
//        tc.setXml(null);
//        tc.setController(null);
//        tc.setServiceImpl(null);
//        tc.setService(null);
        mpg.setTemplate(tc);

        // 策略配置
        mpg.execute();
    }


}